﻿/*
 * Copyright (c)  2009 - Thiago M. Sayão <thiago.sayao@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:

 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * THE SOFTWARE SHALL NOT BE USED TO HOST PRODUCTION SITES WITHOUT
 * PRIOR WRITTEN AUTHORIZATION BY THE AUTHOR.
 */

using System;
using System.Web.Routing;
using System.Web.Mvc;
using System.Web.Security;
using DotNetBurner.Web;
using DotNetBurner.Model;
using System.Web;

namespace DotNetBurner
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_AuthenticateRequest()
        {
            if (User != null)
            {
                //update the user last activity date
                MembershipUser user = Membership.GetUser(true);

                //update the user role
                Repository repo = new Repository(SwissKnife.ConnectionString);

                int userId = repo.GetUserId(user.ProviderUserKey);

                if (userId != 0)
                {
                    repo.UpdateUserRole(userId,
                                        DotNetBurnerSettings.Settings.ModeratorRole,
                                        DotNetBurnerSettings.Settings.PointsToBecomeModerator,
                                        UserPointsSettings.Settings.SubmitStory,
                                        UserPointsSettings.Settings.SubmittedStoryVoted,
                                        UserPointsSettings.Settings.SubmittedStoryPublished,
                                        UserPointsSettings.Settings.SubmittedStoryCommented,
                                        UserPointsSettings.Settings.Comment,
                                        UserPointsSettings.Settings.Vote,
                                        UserPointsSettings.Settings.SubmittedStoryClicked);
                }
            }
        }

        private void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
        }

        static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //Story Related
            routes.MapRouteWithName("Detail", "detail/{id}/{title}", new { controller = "Story", action = "Detail" });
            routes.MapRouteWithName("DetailIdOnly", "detail/{id}", new { controller = "Story", action = "Detail" });
            routes.MapRouteWithName("DetailLegacy", "story/detail/{id}", new { controller = "Story", action = "Detail" });
            routes.MapRouteWithName("DetailLanguage", "en-us/detail/{id}/{title}", new { controller = "Story", action = "LanguageDetail" }); //for backwards compat only
            routes.MapRouteWithName("Upcoming", "upcoming/{sort}/{page}", new { controller = "Story", action = "Upcoming", page = 1, sort = "latest" });
            routes.MapRouteWithName("Search", "search/{page}", new { controller = "Story", action = "Search", page = 1 });
            routes.MapRouteWithName("Submit", "submit", new { controller = "Story", action = "Submit" });
            routes.MapRouteWithName("Comment", "comment", new { controller = "Story", action = "Comment" });
            routes.MapRouteWithName("Edit", "edit/{id}", new { controller = "Story", action = "Edit" });
            routes.MapRouteWithName("Publish", "publish", new { controller = "Story", action = "Publish" });
            routes.MapRouteWithName("Delete", "delete/{id}", new { controller = "Story", action = "Delete" });
            //routes.MapRouteWithName ("SubmitData", "getsubmitdata", new { controller = "Story", action = "GetSubmitData" });
            routes.MapRouteWithName("GetVoteCount", "getvotecount", new { controller = "Story", action = "GetVoteCount" });
            routes.MapRouteWithName("Vote", "vote", new { controller = "Story", action = "Vote" });
            routes.MapRouteWithName("Click", "click", new { controller = "Story", action = "Click" });

            //Feeds
            routes.MapRouteWithName("UpcomingFeed", "feed/{feedType}/upcoming/{sort}/{feedCount}", new { controller = "Story", action = "UpcomingFeed", feedCount = 0, sort = "latest" });
            routes.MapRouteWithName("CategoryFeed", "feed/{feedType}/category/{id}/{name}/{sort}/{feedCount}", new { controller = "Story", action = "CategoryFeed", feedCount = 0, sort = "latest" });
            routes.MapRouteWithName("IndexFeed", "feed/{feedType}/{sort}/{feedCount}", new { controller = "Story", action = "IndexFeed", feedCount = 0, sort = "latest" });
            routes.MapRouteWithName("TagFeed", "feed/{feedType}/tag/{id}/{name}/{sort}/{feedCount}", new { controller = "Story", action = "TagFeed", feedCount = 0, sort = "latest" });
            routes.MapRouteWithName("PostedByFeed", "feed/{feedType}/postedBy/{id}/{name}/{sort}/{feedCount}", new { controller = "Story", action = "PostedByFeed", feedCount = 0, sort = "latest" });

            //User
            routes.MapRouteWithName("Logon", "user/logon", new { controller = "User", action = "Logon" });
            routes.MapRouteWithName("Logoff", "user/logoff", new { controller = "User", action = "Logoff" });
            routes.MapRouteWithName("Signup", "user/signup", new { controller = "User", action = "Signup" });
            routes.MapRouteWithName("SendPassword", "user/sendpassword", new { controller = "User", action = "SendPassword" });
            routes.MapRouteWithName("Profile", "user/profile", new { controller = "User", action = "Profile" });
            routes.MapRouteWithName("ChangeEmail", "user/changeemail", new { controller = "User", action = "ChangeEmail" });
            routes.MapRouteWithName("ChangePassword", "user/changepassword", new { controller = "User", action = "ChangePassword" });
            routes.MapRouteWithName("OpenId", "user/openid", new { controller = "User", action = "OpenId" });

            //Support
            routes.MapRouteWithName("SiteMap", "sitemap", new { controller = "Support", action = "SiteMap" });
            routes.MapRouteWithName("OpenSearch", "opensearch", new { controller = "Support", action = "OpenSearch" });
            routes.MapRouteWithName("Integrate", "integrate", new { controller = "Support", action = "Integrate" });
            routes.MapRouteWithName("Privacy", "privacy", new { controller = "Support", action = "Privacy" });
            routes.MapRouteWithName("FAQ", "faq", new { controller = "Support", action = "FAQ" });

            //Planet
            //routes.MapRouteWithName("PlanetDotNet", "planet", new { controller = "Story", action = "Planet" });
            //routes.MapRouteWithName("PlanetFeed", "planetfeed", new { controller = "Story", action = "PlanetFeed" });

            //Category All
            routes.MapRouteWithName("Category", "category/{id}/{name}/{sort}/{page}", new { controller = "Story", action = "Category", sort = "latest", page = 1 });
            routes.MapRouteWithName("Tag", "tag/{id}/{name}/{sort}/{page}", new { controller = "Story", action = "Tag", sort = "latest", page = 1 });
            routes.MapRouteWithName("PostedBy", "postedby/{id}/{name}/{sort}/{page}", new { controller = "Story", action = "PostedBy", sort = "latest", page = 1 });
            routes.MapRouteWithName("Tags", "tags/{sort}/{page}", new { controller = "Story", action = "Tags", sort = "popular", page = 1 });

            //Index
            routes.MapRouteWithName("Index", "{sort}/{page}", new { controller = "Story", action = "Index", sort = "latest", page = 1 });

            routes.MapRouteWithName("Default", "{controller}/{action}");

            //Catch HTTP Errors
            //routes.MapRoute("Error", "{*url}", new { controller = "Base", action = "Http404" });

            //Change this to allow request for files on disc
            routes.RouteExistingFiles = false;
        }
    }
}